package com.hl.secondHand.common.exception;

import com.hl.secondHand.common.lang.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice   //全局异常捕获
public class GlobalException {

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = ExpiredCredentialsException.class)   //异常类型为token失效异常
    public Result handler(ExpiredCredentialsException e) {
        log.error("token错误或已失效！"); //日志输出
        return new Result(401, "token错误或已失效！请重新登录!", null);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = ShiroException.class)   //异常类型为shiro异常
    public Result handler(ShiroException e) {
        log.error("shiro异常"); //日志输出
        return new Result(401, "尚未登录，请先登录再进行操作!", null);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = RuntimeException.class)   //异常类型为运行时异常
    public Result handler(RuntimeException e) {
        log.error("运行时异常/n"+e.getMessage()); //日志输出
        return new Result(500,"系统异常，请联系管理员！",null);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})   //异常类型为实体校验异常
    public Result handler() {
        log.error("实体校验异常"); //日志输出
        return Result.fail("实体校验异常");
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(value = IllegalArgumentException.class)   //异常类型为Assert异常
    public Result handler(IllegalArgumentException e) {
        log.error("Assert异常"); //日志输出
        return Result.fail(e.getMessage());
    }


}
